using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// The Tab class is a single tab item on a TabBlock control. It allows
	/// paging on multi-page forms, and can be secured to groups/permissions
	/// etc. It must be linked to a TabContainer object.
	/// </summary>
	public class Tab : IStateManager
	{
		#region Tab Properties
		/// <summary>
		/// Title specifies the text that appears in the tab
		/// </summary>
		[
		Category("Display"),
		Description("Specifies the title of the tab")
		]
		public string Title
		{
			get
			{
				object t = ViewState["Title"];
				if( t == null ) return String.Empty;
				return (string) t;
			}
			set
			{
				ViewState["Title"] = value;
			}
		}
		/// <summary>
		/// Specifies the ID of the TabContainer object this tab will control
		/// </summary>
		[Category("Linkage"), Description("Specifies the ID of the TabContainer object to control")]
		public string ContainerID
		{
			get
			{
				object t = ViewState["ContainerID"];
				if( t == null ) return String.Empty;
				return (string) t;
			}
			set
			{
				ViewState["ContainerID"] = value;
			}
		}
		#endregion
		#region IStateManager Members
		/// <summary>
		/// Private member indicating whether or not viewstate is being tracked
		/// </summary>
		private bool _isTrackingViewState;
		/// <summary>
		/// StateBag to hold the custom viewstate
		/// </summary>
		private StateBag _viewState;
		/// <summary>
		/// This property implements the ViewState object, a state bag that participates
		/// in state management and can be posted during round-trips....this property may only
		/// be accessed after the start of the tracking view state phase, so we need to check
		/// if we're now tracking and if so, tell our system to start tracking viewstate.
		/// </summary>
		protected StateBag ViewState
		{
			get
			{
				if( _viewState == null )
				{
					_viewState = new StateBag(false);
					if( _isTrackingViewState ) ((IStateManager) _viewState).TrackViewState();
				}

				return _viewState;
			}
		}
		/// <summary>
		/// Returns true or false depending on whether we're monitoring
		/// our viewstate for changes...
		/// </summary>
		public bool IsTrackingViewState
		{
			get
			{
				return _isTrackingViewState;
			}
		}
		/// <summary>
		/// Save the contents of our viewstate into the viewstate serialisation
		/// stream by returning the object.
		/// </summary>
		/// <returns></returns>
		public object SaveViewState()
		{
			if( _viewState != null ) return ((IStateManager) _viewState).SaveViewState();
			return null;
		}
		/// <summary>
		/// Load our viewstate from the object passed up to us.
		/// </summary>
		/// <param name="state"></param>
		public void LoadViewState(object state)
		{
			if( state != null )
			{
				((IStateManager)ViewState).LoadViewState(state);
			}
		}
		/// <summary>
		/// Enforce tracking of the view state now.
		/// </summary>
		public void TrackViewState()
		{
			_isTrackingViewState = true;
			if( _viewState != null ) ((IStateManager)_viewState).TrackViewState();
		}		
		/// <summary>
		/// Sets the viewstate keys to dirty indicating they
		/// need to be updated during the save viewstate phase...
		/// </summary>
		internal void SetDirty()
		{
			if( _viewState != null )
			{
				ICollection keys = _viewState.Keys;
				foreach( string key in keys )
				{
					_viewState.SetItemDirty(key, true);
				}
			}
		}
		#endregion
	}
}
